
package com.bcurtu.db;

import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AccountDaoHibernate extends HibernateDaoSupport implements AccountDao{

	public int blockAccount(String alias) {
		 List<Account> list=getHibernateTemplate().find(
				 "FROM Account WHERE alias=?",alias);
		 for (Account acc:list){
			 acc.setActive(false);
			 getHibernateTemplate().update(acc);
		 }
		return list.size();
	}

	public int deleteExpiredAccounts(Date date) {
		 List<Account> list=getHibernateTemplate().find(
				 "FROM Account WHERE active=false AND expirationDate<=?",date);
		 for (Account acc:list){
			 getHibernateTemplate().delete(acc);
		 }
		return list.size();
	}

	public int forceDeleteAccount(String alias) {
		 List<Account> list=getHibernateTemplate().find(
				 "FROM Account WHERE alias=?",alias);
		 for (Account acc:list){
			 getHibernateTemplate().delete(acc);
		 }
		return list.size();
	}

	public List<Account> getBlockingAccounts(Date date) {
		 List<Account> list=getHibernateTemplate().find(
				 "from Account where active=True AND blockingDate<=?",date);
		 return list;
	}

	public void insertAccount(Account acc) {
		getHibernateTemplate().save(acc);
		
	}
	
	public void updateAccount(Account acc) {
		getHibernateTemplate().update(acc);
		
	}
	

	public boolean isValidAlias(String alias) {
		List l= getHibernateTemplate().find("FROM Account WHERE alias=?",alias);
		return l==null || l.size()==0;
	}

	public Account getAccountByAliasAndForward(String alias, String forward) {
		List<Account> l= getHibernateTemplate().find("FROM Account WHERE alias=? AND forward=?",new Object[]{alias,forward});
		if (l.size()==0){
			return null;
			
		} else {
			return l.get(0);
		}
	}

	
	
}
